package com.peersafe.aliyun.aliyun_plugin;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

import com.aliyun.aliyunface.api.ZIMFacade;
import com.aliyun.aliyunface.api.ZIMCallback;
import com.aliyun.aliyunface.api.ZIMFacadeBuilder;
import com.aliyun.aliyunface.api.ZIMResponse;
import com.aliyun.aliyunface.api.ZIMCallback;

import android.content.ContextWrapper;
import android.app.Application;
import android.content.Context;
import android.app.Activity;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
/** AliyunPlugin */
public class AliyunPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  private Application myApplication;
  private static final String PLUGIN_NAME = "aliyun_plugin";
  private ZIMFacade zimFacade;
  private Result result;
  private static Activity activity;
  @Override
  public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
    final MethodChannel channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), PLUGIN_NAME);
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onAttachedToActivity(ActivityPluginBinding binding) {
    System.out.println("onAttachedToActivity");
    activity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {}

  @Override
  public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {}

  @Override
  public void onDetachedFromActivity() {}

  @Override
  public void onMethodCall(MethodCall call,Result result) {
    this.result = result;
    System.out.println(call.method);
    if (call.method.equals("metaInfo")) {
        String metaInfo = ZIMFacade.getMetaInfos(activity);
        result.success(metaInfo);
    }else if (call.method.equals("init")) {
      ZIMFacade.install(activity);
      zimFacade = ZIMFacadeBuilder.create(activity);
      result.success(null);
    }else if (call.method.equals("face")) {
      try{
        String certifyId = call.argument("certifyId");
        System.out.println(certifyId);
        zimFacade.verify(certifyId, true, getFaceCallback());
      }catch(Error e){
        result.success(e.toString());
      }
        
    }else {
        result.notImplemented();
    }
  }
  private ZIMCallback getFaceCallback() {
      return new ZIMCallback() {
          @Override
          public boolean response(final ZIMResponse response) {
              if (1000 == response.code) {
                  result.success("success");
              } else {
                  result.success("fail");
              }
              return true;
          }
      };
  } 

  @Override
  public void onDetachedFromEngine(FlutterPluginBinding binding) {
  }
}
